<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Main extends CI_Controller
{

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct()
    {
        parent::__construct();
        $this->data['nama_akun'] = $this->session->userdata('username');
    }
    
    public function index()
    {
        $this->load->view('index');
    }

    function beranda()
    {
        $this->is_logged_in();

        $data['nama_akun'] = $this->session->userdata('username');

        $data['main_content'] = 'beranda';
        $this->load->view('include/template', $data);
    }

    function verify()
    {
        $this->load->model('members');
        $query = $this->members->validasi();

        $row = $this->members->get_login()->row();

        $username = $this->input->post('username');
        $password = $this->input->post('password');

        if (empty($username) && empty($password)) {
            $this->messages->add('Data login kosong. ', 'error');
            redirect('main/index', 'refresh');
        } else {
            if ($query) {
                $usertype = $row->type;
                $accid = $row->acc_id;
                $data = array(
                    'username' => $row->acc_name,
                    'tipe' => $usertype,
                    'accid' => $accid,
                    'is_logged_in' => true
                );

                $this->session->set_userdata($data);

                $action = "$username berhasil login";
                $this->members->save_log($action);

                redirect('main/beranda', 'refresh');
            } else {
                redirect('main/index', 'refresh');
                $this->messages->add('Wrong info. ', 'error');
            }
        }
    }

    public function admin()
    {
        $this->is_logged_in();
        $this->load->model('members');

        $data['nama_akun'] = $this->session->userdata('username');

        $data['accounts'] = $this->members->get_users()->result();

        $data['main_content'] = 'admin_home';
        $this->load->view('include/template', $data);
    }

    public function tambah()
    {
        $this->is_logged_in();

        $data['values'] = null;
        $data['id'] = null;
        $data['action'] = 'save';
        $data['buttonVal'] = 'Tambah';

        $data['nama_akun'] = $this->session->userdata('username');

        $data['main_content'] = 'admin_tambah';
        $this->load->view('include/template', $data);
    }

    public function saveAcc()
    {
        $this->is_logged_in();

        $this->load->library('form_validation');

        $this->form_validation->set_rules('namaAkun', 'Nama Akun', 'trim|required');
        $this->form_validation->set_rules('namaPengguna', 'Nama Pengguna', 'trim|required|min_length[4]|xss_clean');
        $this->form_validation->set_rules('nokontak', 'Kontak', 'trim|required');
        $this->form_validation->set_rules('tipe', 'Tipe', 'required');

        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[password2]');
        $this->form_validation->set_rules('password2', 'Konfirmasi Password', 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $this->messages->add('Data tidak komplit, silahkan diulang. ', 'error');
            $this->tambah();
        } else {

            $this->load->model('members');

            if ($query = $this->members->TambahUser()) {
                $this->messages->add('Tambah user baru berhasil. ', 'success');
                redirect('main/admin', 'refresh');
            } else {
                $this->messages->add('Tambah user baru gagal, silahkan diulang. ', 'error');
                redirect('main/tambah', 'refresh');
            }
        }
    }

    public function ubah($id)
    {
        $this->is_logged_in();
        $this->load->model('members');
        $data['action'] = 'update';
        $data['buttonVal'] = 'Ubah';

        $obj = $this->members->get_user($id)->row();

        $data['id'] = $id;
        $data['values']['namaAkun'] = $obj->acc_name;
        $data['values']['namaPengguna'] = $obj->user_name;
        $data['values']['nokontak'] = $obj->contact;
        $data['values']['tipe'] = $obj->type;

        $data['nama_akun'] = $this->session->userdata('username');

        $data['main_content'] = 'admin_tambah';
        $this->load->view('include/template', $data);
    }

    public function updateAcc()
    {
        $this->is_logged_in();

        $this->load->library('form_validation');

        $id = $this->input->post('id');

        $this->form_validation->set_rules('namaAkun', 'Nama Akun', 'trim|required');
        $this->form_validation->set_rules('namaPengguna', 'Nama Pengguna', 'trim|required|min_length[4]|xss_clean');
        $this->form_validation->set_rules('nokontak', 'Kontak', 'trim|required');
        $this->form_validation->set_rules('tipe', 'Tipe', 'required');

        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[password2]');
        $this->form_validation->set_rules('password2', 'Konfirmasi Password', 'trim|required');


        if ($this->form_validation->run() == FALSE) {
            $this->messages->add('Data tidak komplit, silahkan diulang. ', 'error');
            $this->ubah($id);
        } else {

            $this->load->model('members');

            if ($query = $this->members->ubahUser($id)) {
                $this->messages->add('Ubah user berhasil. ', 'success');
                redirect('main/admin', 'refresh');
            } else {
                $this->messages->add('Ubah user gagal, silahkan diulang. ', 'error');
                redirect('main/edit/' . $id, 'refresh');
            }
        }
    }

    public function hapus($id)
    {
        $this->is_logged_in();
        $this->load->model('members');

        $obj = $this->members->get_user($id)->row();

        $nama = $obj->acc_name;
        $creator = $this->session->userdata('username');

        $query = $this->members->hapusUser($id);

        if ($query) {
            $action = "$creator berhasil menghapus akun $nama";
            $this->members->save_log($action);

            $this->messages->add("User $nama Berhasil di hapus", 'success');
            redirect('main/admin', 'refresh');
        } else {
            $this->messages->add("User $nama gagal di hapus", 'error');
            redirect('main/admin', 'refresh');
        }
    }

    public function penjualan()
    {
        $this->is_logged_in();

        $data['nama_akun'] = $this->session->userdata('username');

        $data['bulan'] = '';
        $data['tahun'] = '';
        $data['styleDisplay'] = 'style="display:none;"';
        $data['tipe'] = '';
        $data['restoran'] = '';

        $data['main_content'] = 'penjualan';
        $this->load->view('include/template', $data);
    }

    public function testdummy()
    {
        $data['nama_akun'] = '';

        $data['main_content'] = 'mychart';
        $this->load->view('include/template', $data);
    }

    public function reports()
    {
        $this->is_logged_in();
        $data = array();

        $data['nama_akun'] = $this->session->userdata('username');
        $data['styleDisplay'] = '';

        $this->load->library('cafebz');
        $bulan = $this->input->post('month');
        $tahun = $this->input->post('year');
        $restoran = $this->input->post('restoran');
        $tipe = $this->input->post('type');
        $query = null;

        $data['bulan'] = $bulan;
        $data['tahun'] = $tahun;
        $data['displayHidden'] = '';

        if ($tipe == 's') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_sales_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Penjualan';
            $data['sales'] = $query;
            $namaResto = strtoupper($restoran);
        } else if ($tipe == 'p') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_payment_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Pembayaran';
            $data['payment'] = $query;
            $data['displayHidden'] = 'style="display:none";';
            $namaResto = strtoupper($restoran);
        }

        $data['restoran'] = $namaResto;

        $this->session->set_userdata('bulan', $bulan);
        $this->session->set_userdata('tahun', $tahun);
        $this->session->set_userdata('tipe', $tipe);
        $this->session->set_userdata('restoran', $restoran);
        $this->session->set_userdata('namaResto', $namaResto);

        $data['main_content'] = 'penjualan/summary';
        $this->load->view('include/template', $data);
    }

    public function summary()
    {
        $this->is_logged_in();
        $data = array();

        $data['nama_akun'] = $this->session->userdata('username');
        $data['styleDisplay'] = '';

        $this->load->library('cafebz');
        $bulan = $this->session->userdata('bulan');
        $tahun = $this->session->userdata('tahun');
        $restoran = $this->session->userdata('restoran');
        $namaResto = $this->session->userdata('namaResto');
        $tipe = $this->session->userdata('tipe');
        $query = null;

        $data['bulan'] = $bulan;
        $data['tahun'] = $tahun;
        $data['displayHidden'] = '';

        if ($tipe == 's') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_sales_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Penjualan';
            $data['sales'] = $query;
            $namaResto = strtoupper($restoran);
        } else if ($tipe == 'p') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_payment_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Pembayaran';
            $data['payment'] = $query;
            $data['displayHidden'] = 'style="display:none";';
            $namaResto = strtoupper($restoran);
        }

        $data['restoran'] = $namaResto;

        $data['main_content'] = 'penjualan/summary';
        $this->load->view('include/template', $data);
    }

    public function grafikKategori()
    {
        $this->is_logged_in();
        $data = array();

        $data['nama_akun'] = $this->session->userdata('username');
        $data['styleDisplay'] = '';

        $this->load->library('cafebz');
        $bulan = $this->session->userdata('bulan');
        $tahun = $this->session->userdata('tahun');
        $restoran = $this->session->userdata('restoran');
        $namaResto = $this->session->userdata('namaResto');
        $tipe = $this->session->userdata('tipe');
        $query = null;

        $data['bulan'] = $bulan;
        $data['tahun'] = $tahun;
        $data['displayHidden'] = '';

        if ($tipe == 's') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_sales_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Penjualan';
            $data['sales'] = $query;
            $namaResto = strtoupper($restoran);
        } else if ($tipe == 'p') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_payment_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Pembayaran';
            $data['payment'] = $query;
            $data['displayHidden'] = 'style="display:none";';
            $namaResto = strtoupper($restoran);
        }

        $data['restoran'] = $namaResto;

        $data['main_content'] = 'penjualan/grafikKategori';
        $this->load->view('include/template', $data);
    }

    public function grafikKumulatif()
    {
        $this->is_logged_in();
        $data = array();

        $data['nama_akun'] = $this->session->userdata('username');
        $data['styleDisplay'] = '';

        $this->load->library('cafebz');
        $bulan = $this->session->userdata('bulan');
        $tahun = $this->session->userdata('tahun');
        $restoran = $this->session->userdata('restoran');
        $namaResto = $this->session->userdata('namaResto');
        $tipe = $this->session->userdata('tipe');
        $query = null;

        $data['bulan'] = $bulan;
        $data['tahun'] = $tahun;
        $data['displayHidden'] = '';

        if ($tipe == 's') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_sales_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Penjualan';
            $data['sales'] = $query;
            $namaResto = strtoupper($restoran);
        } else if ($tipe == 'p') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_payment_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Pembayaran';
            $data['payment'] = $query;
            $data['displayHidden'] = 'style="display:none";';
            $namaResto = strtoupper($restoran);
        }

        $data['restoran'] = $namaResto;

        $data['main_content'] = 'penjualan/grafikKumulatif';
        $this->load->view('include/template', $data);
    }

    public function grafikHarian()
    {
        $this->is_logged_in();
        $data = array();

        $data['nama_akun'] = $this->session->userdata('username');
        $data['styleDisplay'] = '';

        $this->load->library('cafebz');
        $bulan = $this->session->userdata('bulan');
        $tahun = $this->session->userdata('tahun');
        $restoran = $this->session->userdata('restoran');
        $namaResto = $this->session->userdata('namaResto');
        $tipe = $this->session->userdata('tipe');
        $query = null;

        $data['bulan'] = $bulan;
        $data['tahun'] = $tahun;
        $data['displayHidden'] = '';

        if ($tipe == 's') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_sales_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Penjualan';
            $data['sales'] = $query;
            $namaResto = strtoupper($restoran);
        } else if ($tipe == 'p') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_payment_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Pembayaran';
            $data['payment'] = $query;
            $data['displayHidden'] = 'style="display:none";';
            $namaResto = strtoupper($restoran);
        }

        $data['restoran'] = $namaResto;

        $data['main_content'] = 'penjualan/grafikHarian';
        $this->load->view('include/template', $data);
    }

    public function generate_report()
    {
        $this->is_logged_in();
        $data = array();

        $this->load->library('cafebz');
        $bulan = $this->session->userdata('bulan');
        $tahun = $this->session->userdata('tahun');
        $restoran = $this->session->userdata('restoran');
        $namaResto = $this->session->userdata('namaResto');
        $tipe = $this->session->userdata('tipe');
        $query = null;
        $filename = null;

        $data['bulan'] = $bulan;
        $data['tahun'] = $tahun;

        $bulan1 = null;

        switch (strtolower($bulan)) {
            case null: break;
            case "1": $bulan1 = "Januari";
                break;
            case "2": $bulan1 = "Februari";
                break;
            case "3": $bulan1 = "Maret";
                break;
            case "4": $bulan1 = "April";
                break;
            case "5": $bulan1 = "Mei";
                break;
            case "6": $bulan1 = "Juni";
                break;
            case "7": $bulan1 = "Juli";
                break;
            case "8": $bulan1 = "Agustus";
                break;
            case "9": $bulan1 = "September";
                break;
            case "10": $bulan1 = "Oktober";
                break;
            case "11": $bulan1 = "Nopember";
                break;
            case "12": $bulan1 = "Desember";
                break;
        }

        if ($tipe == 's') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_sales_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_sales_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Penjualan';
            $data['sales'] = $query;
            $namaResto = strtoupper($restoran);
            $filename = "$namaResto (Summary Sales Bulan $bulan1 Tahun $tahun";
        } else if ($tipe == 'p') {

            switch ($restoran) {
                case "sf": $query = $this->cafebz->sf_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "cox": $query = $this->cafebz->cox_payment_monthly((int) $bulan, (int) $tahun);
                    break;
                case "velpa": $query = $this->cafebz->velpa_payment_monthly((int) $bulan, (int) $tahun);
                    break;
            }

            $data['tipe'] = 'Pembayaran';
            $data['payment'] = $query;
            $namaResto = strtoupper($restoran);
            $data['displayHidden'] = 'style="display:none";';
            $filename = "$namaResto (Summary Payment Bulan $bulan1 Tahun $tahun";
        }

        $data['restoran'] = $namaResto;

        header('Content-type: application/ms-excel');
        header("Content-Disposition: attachment; filename=$filename.xls");

        $this->load->view('hasil_download', $data);
    }

    public function editSales()
    {
        $this->is_logged_in();
        $post = $this->input->post();
        if (!$post) {
            redirect('main/penjualan');
        }
        
        $date = explode('/', $post['date']);
        $time = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
        $resto = $this->session->userdata('restoran');

        // Update values
        if (isset($post['submit'])) {
            $data = array(
                'beverage' => $post['beverage'],
                'food' => $post['food'],
                'tobaco' => $post['tobaco'],
                'others' => $post['others'],
                'discount_customer' => $post['discount_customer'],
                'discount_ocheck' => $post['discount_ocheck'],
                'rounding' => $post['rounding'],
                'tax' => $post['tax'],
                'service' => $post['service'],
                'modified_by' => $this->session->userdata('accid')
            );
            if($resto == 'velpa') $data['wine_takeaway'] = $post['wine_takeaway'];
            
            $this->load->library('cafebz');
            if($resto == 'velpa') $this->cafebz->velpa_sales_update($time,$data);
            else if($resto == 'sf') $this->cafebz->sf_sales_update($time,$data);
            else if($resto == 'cox') $this->cafebz->cox_sales_update($time,$data);
            
            $this->messages->add("Data sales {$resto} pada tanggal {$post['date']} telah diubah", 'success');
            redirect('main/penjualan');
        }

        // Load account, if exists
        $acc = null;
        if ($post['accid'] != '') {
            $this->load->model('members');
            $acc = $this->members->get_user($post['accid'])->row_array();
        }

        // Load data
        $this->load->library('cafebz');
        if ($resto == 'sf') {
            $this->data['sales_ori'] = $this->cafebz->sf_sales_daily($time, true);
            $this->data['sales_mod'] = $this->cafebz->sf_sales_daily($time);
        } else if ($resto == 'cox') {
            $this->data['sales_ori'] = $this->cafebz->cox_sales_daily($time, true);
            $this->data['sales_mod'] = $this->cafebz->cox_sales_daily($time);
        } else if ($resto == 'velpa') {
            $this->data['sales_ori'] = $this->cafebz->velpa_sales_daily($time, true);
            $this->data['sales_mod'] = $this->cafebz->velpa_sales_daily($time);
        }

        $this->data['date'] = $post['date'];
        $this->data['resto'] = $resto;
        $this->data['acc'] = $acc;
        $this->data['main_content'] = 'penjualan/editSales';
        $this->load->view('include/template', $this->data);
    }

    public function editPayment()
    {
        $this->is_logged_in();
        $post = $this->input->post();
        if (!$post) {
            redirect('main/penjualan');
        }
        
        $date = explode('/', $post['date']);
        $time = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
        $resto = $this->session->userdata('restoran');

        // Update values
        if (isset($post['submit'])) {
            $data = array(
                'cash' => $post['cash'],
                'visa' => $post['visa'],
                'master' => $post['master'],
                'bca_card' => $post['bca_card'],
                'debit' => $post['debit'],
                'city_ledger' => $post['city_ledger'],
                'voucher' => $post['voucher'],
                'modified_by' => $this->session->userdata('accid')
            );
            
            $this->load->library('cafebz');
            if($resto == 'velpa') $this->cafebz->velpa_payment_update($time,$data);
            else if($resto == 'sf') $this->cafebz->sf_payment_update($time,$data);
            else if($resto == 'cox') $this->cafebz->cox_payment_update($time,$data);
            
            $this->messages->add("Data payment {$resto} pada tanggal {$post['date']} telah diubah", 'success');
            redirect('main/penjualan');
        }

        // Load account, if exists
        $acc = null;
        if ($post['accid'] != '') {
            $this->load->model('members');
            $acc = $this->members->get_user($post['accid'])->row_array();
        }

        // Load data
        $this->load->library('cafebz');
        if ($resto == 'sf') {
            $this->data['payment_ori'] = $this->cafebz->sf_payment_daily($time, true);
            $this->data['payment_mod'] = $this->cafebz->sf_payment_daily($time);
        } else if ($resto == 'cox') {
            $this->data['payment_ori'] = $this->cafebz->cox_payment_daily($time, true);
            $this->data['payment_mod'] = $this->cafebz->cox_payment_daily($time);
        } else if ($resto == 'velpa') {
            $this->data['payment_ori'] = $this->cafebz->velpa_payment_daily($time, true);
            $this->data['payment_mod'] = $this->cafebz->velpa_payment_daily($time);
        }

        $this->data['date'] = $post['date'];
        $this->data['resto'] = $resto;
        $this->data['acc'] = $acc;
        $this->data['main_content'] = 'penjualan/editPayment';
        $this->load->view('include/template', $this->data);
    }

    public function inventaris()
    {
        $this->is_logged_in();

        $data['nama_akun'] = $this->session->userdata('username');

        $data['main_content'] = 'inventaris';
        $this->load->view('include/template', $data);
    }

    public function pembelian()
    {
        $this->is_logged_in();

        $data['nama_akun'] = $this->session->userdata('username');

        $data['main_content'] = 'pembelian';
        $this->load->view('include/template', $data);
    }

    public function log($offset = 0)
    {
        $this->is_logged_in();
        $this->session->unset_userdata('cariAkun');
        $this->session->unset_userdata('dari');
        $this->session->unset_userdata('sampai');

        $this->load->model('members');

        $limit = 10;

        $data['nama_akun'] = $this->session->userdata('username');

        $data['logs'] = $this->members->get_all_logs($limit, $offset)->result();

        $uri_segment = 3;
        // where this page begins
        $offset = $this->uri->segment($uri_segment);

        $this->load->library('pagination');
        $config['uri_segment'] = $uri_segment;
        $config['base_url'] = site_url('main/log/');
        $config['total_rows'] = $this->members->total_logs();
        $config['per_page'] = 10;
        $config['num_links'] = 1;

        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        $data['main_content'] = 'log';
        $this->load->view('include/template', $data);
    }

    function filter_data()
    {
        $this->is_logged_in();
        $this->load->model('members');

        $query = null;
        $action = null;

        $creator = $this->session->userdata('username');

        $cariAkun = $this->input->post('cariAkun');
        $dari = $this->input->post('dari');
        $sampai = $this->input->post('sampai');

        $this->session->set_userdata('cariAkun', $cariAkun);
        $this->session->set_userdata('dari', $dari);
        $this->session->set_userdata('sampai', $sampai);

        if (empty($cariAkun) && empty($dari) && empty($sampai)) {
            $query = $this->members->semua_log();
            $action = "$creator mencari semua log";
        } else {

            if (empty($cariAkun)) {
                $action = "$creator mencari semua log dari tanggal = $dari, sampai tanggal = $sampai";
            } elseif (empty($dari) && empty($sampai)) {
                $action = "$creator mencari semua log akun = $cariAkun";
            } else {
                $action = "$creator mencari log akun = $cariAkun, dari tanggal = $dari, sampai tanggal = $sampai";
            }

            $query = $this->members->cariUser($cariAkun, $dari, $sampai);
        }



        if ($query) {
            $this->members->save_log($action);
            redirect('main/hasil_filter', 'refresh');
        } else {

            $data['nama_akun'] = $this->session->userdata('username');

            $data['logs'] = null;

            $data['main_content'] = 'hasil_cari';
            $this->load->view('include/template', $data);
        }
    }

    function hasil_filter($offset = 0)
    {
        $this->is_logged_in();
        $this->load->model('members');

        $limit = 10;

        $cariAkun = $this->session->userdata('cariAkun');
        $dari = $this->session->userdata('dari');
        $sampai = $this->session->userdata('sampai');

        if (empty($cariAkun) && empty($dari) && empty($sampai)) {
            $query = $this->members->get_all_logs($limit, $offset);
            $total_rows = $this->members->semua_log()->num_rows();
        } else {

            $query = $this->members->hasilCari($cariAkun, $dari, $sampai, $limit, $offset);
            $total_rows = $this->members->total_hasilcari_logs($cariAkun, $dari, $sampai);
        }

        $data['nama_akun'] = $this->session->userdata('username');

        $data['logs'] = $query->result();

        $uri_segment = 3;
        // where this page begins
        $offset = $this->uri->segment($uri_segment);

        $this->load->library('pagination');
        $config['uri_segment'] = $uri_segment;
        $config['base_url'] = site_url('main/hasil_filter/');
        $config['total_rows'] = $total_rows;
        $config['per_page'] = 10;

        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        $data['main_content'] = 'hasil_cari';
        $this->load->view('include/template', $data);
    }

    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');

        if (!isset($is_logged_in) || $is_logged_in != true) {
            $this->messages->add('Silahkan login terlebih dahulu.', 'error');
            redirect('main/index', 'refresh');
        }
    }

    function log_out()
    {

        $this->messages->add('Anda berhasil logout', 'success');

        $username = $this->session->userdata('username');

        $this->load->model('members');
        $action = "$username berhasil log out";

        $this->members->save_log($action);

        $this->session->unset_userdata('username');
        $this->session->unset_userdata('is_logged_in');
        $this->session->sess_destroy();
        $this->session->sess_create();

        redirect('main/index', 'refresh');
    }

}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */